<template>
	<view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>
<script>
	import site from '@/custom/siteroot.js';
	import Socket from "@/common/socket.js"
	export default {
		data() {
			return {
				adminInfo: uni.getStorageSync("adminInfo"),
				notificationInterval: null
			}
		},
		globalData: {
			version: '1.3.2',
			storeName: '',
			checked: [],
			userInfo: {},
			onImgurl: `${site.index}/storage/default/drag/0/static/`,
		},
		onLaunch: function() {
			uni.$on('socketInit', this.socketMsg);
			this.registBackgroundMusic()
			let token = uni.getStorageSync('token'),
				storeId = uni.getStorageSync('storeId'),
				uniacid = uni.getStorageSync('uniacid');
			if (token && storeId && uniacid) {
				this.socketMsg()
			}
			const menus = uni.getStorageSync('menus');
			if (menus) {
				// 将数据恢复到Vuex状态中
				this.$store.commit('setMenus', menus);
			}
		},
		onShow: function() {
			this.checkForUpdate();
			// this.notificationInterval = setInterval(() => {
			// 	this.getNotification();
			// }, 50000);
		},
		onHide: function() {
			clearInterval(this.notificationInterval);
			this.notificationInterval = null;
		},
		methods: {
			registBackgroundMusic() {
				// #ifdef H5
				this.bgAudioMannager = uni.createInnerAudioContext();
				// #endif
				// #ifdef  APP-PLUS || MP-WEIXIN
				this.bgAudioMannager = uni.getBackgroundAudioManager();
				// #endif
				this.bgAudioMannager.title = '订单提醒';
			},
			socketMsg() {
				// #ifdef APP-PLUS 
				let sroot = uni.getStorageSync('siteroot')
				// #endif
				// #ifndef APP-PLUS 
				let sroot = site.siteroot
				// #endif
				let wsUrl = sroot.replace(/(https|http)/, 'wss'),
					chatConfig = {}
				chatConfig.url = `${wsUrl}/ws`
				this.socket = new Socket(chatConfig)
				var addNum = 0,
					voiceList = [],
					autoText = this.bgAudioMannager
				console.log('msrm', this.bgAudioMannager)
				this.socket.onMessage((msg) => {
					console.log('ping', msg)
					if (msg.type && msg.type == 'voice') {
						console.log('voice', msg)
						var msgs = msg.msg,
							voiceNum = msg.msg.num
						autoText.src = msgs.voiceUrl
						autoText.onCanplay(a => {
							autoText.play()
							// console.log('play',msgs)
						});
						autoText.onEnded((res) => {
							// console.log('end',voiceNum)
							if (voiceNum >= 1) {
								autoText.play()
								voiceNum--
							} else {
								// #ifdef H5
								autoText.destroy()
								autoText = uni.createInnerAudioContext()
								// #endif
							}
						})
						autoText.onError((res) => {
							console.log('errMsg', res.errMsg);
							console.log('errCode', res.errCode);
						});
					}
				})
			},
			checkForUpdate() {
				const _this = this
				const updateManager = uni.getUpdateManager();
				updateManager.onCheckForUpdate((res) => {
					if (res && res.hasUpdate) {
						uni.showModal({
							title: '更新提示',
							content: '检测到新版本，是否下载新版本并重启小程序？',
							success(res) {
								if (res.confirm) {
									_this.downLoadAndUpdate(updateManager)
								} else {
									uni.showModal({
										title: '温馨提示~',
										content: '本次版本更新涉及到新的功能添加，旧版本无法正常访问的哦~',
										confirmText: "确定更新",
										cancelText: "取消更新",
										success(res) {
											if (res.confirm) {
												_this.downLoadAndUpdate(updateManager)
											}
										}
									});
								}
							}
						});
					}
				});
			},
			downLoadAndUpdate(updateManager) {
				const _this = this
				uni.showLoading({
					title: '小程序更新中'
				});
				updateManager.onUpdateReady((res) => {
					console.log('onUpdateReady-res', res);
					uni.hideLoading();
					updateManager.applyUpdate()
				});

				// 更新失败
				updateManager.onUpdateFailed((res) => {
					console.log('onUpdateFailed-res', res);
					uni.hideLoading();
					uni.showModal({
						title: '已经有新版本了哟~',
						content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~',
						showCancel: false
					});
				});
			},
			getNotification() {
				this.beg.request({
					url: '/admin/notification.Index/push',
					method: 'get',
				}).then(res => {
					if (res.code == 1) {
						if (res.data != null && res.data != '') {
							const backgroundAudioManager = wx.getBackgroundAudioManager({
								useWebAudioImplement: true
							});
							backgroundAudioManager.src = site.siteroot + '/static/sounds/' + res.data.sound +
								'.mp3';
							backgroundAudioManager.title = res.data.msg;
							backgroundAudioManager.singer = '快抖生活';
							backgroundAudioManager.onCanplay(() => {
								backgroundAudioManager.play();
							});
							backgroundAudioManager.onError((res) => {
								console.log('音频播放出错:', res.errMsg);
							});
						}
					}
				});
			},
		},
	}
</script>

<style lang="scss">
	@import "@/common/icon/iconfont.css";
	@import './common/styles/index.css';
	@import './common/styles/my.css';
	@import "@/uni_modules/uview-ui/index.scss";
	@import "@/common/icons/iconfont.css";
</style>